package com.example.basic.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;
import java.util.Map;

import static com.alibaba.fastjson.JSON.DEFAULT_GENERATE_FEATURE;

/**
 * Json相关的工具类
 */
@SuppressWarnings({"unchecked"})
public class JsonUtils {
    private final static ObjectMapper objectMapper = new ObjectMapper();

    private JsonUtils() {
    }

    public static ObjectMapper getObjectMapper() {
        return objectMapper;
    }

    public static String toJson(Object obj) throws Exception {
        return getObjectMapper().writeValueAsString(obj);
    }

    public static <T> T toObject(String json, Class<T> clazz) throws Exception {
        return getObjectMapper().readValue(json, clazz);
    }

    public static Map<String, Object> toMap(String json) throws Exception {
        return getObjectMapper().readValue(json, Map.class);
    }

    public static <T> T mapToObject(Map map, Class<T> clazz) {
        return getObjectMapper().convertValue(map, clazz);
    }

    /**
     *
     * 功能描述：把java对象转换成JSON数据（可选择配置）
     * @param object
     * @param config
     * @param filters
     * @param dateFormat
     * @param defaultFeatures
     * @param features	SerializerFeature有用的一些枚举值：
     * 			QuoteFieldNames———-输出key时是否使用双引号,默认为true
    WriteMapNullValue——–是否输出值为null的字段,默认为false
    WriteNullNumberAsZero—-数值字段如果为null,输出为0,而非null
    WriteNullListAsEmpty—–List字段如果为null,输出为[],而非null
    WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,而非null
    WriteNullBooleanAsFalse–Boolean字段如果为null,输出为false,而非null
     * @return
     */
    public static String toJsonString(Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, Integer defaultFeatures, SerializerFeature... features) {
        if(config == null){
            config = SerializeConfig.globalInstance;
        }
        if(filters == null){
            filters = new SerializeFilter[0];
        }
        if(defaultFeatures == null){
            defaultFeatures = DEFAULT_GENERATE_FEATURE;
        }
        if(features == null){
            return JSON.toJSONString(object,config,filters,dateFormat,defaultFeatures);
        }
        return JSON.toJSONString(object,config,filters,dateFormat,defaultFeatures,features);
    }

    /**
     * 功能描述：把JSON数据转换成指定的java对象列表
     *
     * @param jsonData
     *            JSON数据
     * @param clazz
     *            指定的java对象
     * @return
     * @throws Exception
     */
    public static <T> List<T> toBeanList(String jsonData, Class<T> clazz) {
        return JSON.parseArray(jsonData, clazz);
    }

    /**
     * 功能描述：把JSON数据转换成指定的java对象
     *
     * @param jsonData
     *            JSON数据
     * @param clazz
     *            指定的java对象
     * @return
     * @throws Exception
     */
    public static <T> T toSingleBean(String jsonData, Class<T> clazz) {
        return JSON.parseObject(jsonData, clazz);
    }

    /**
     * 功能描述：把java对象转换成JSON数据
     *
     * @param object
     *            java对象
     * @return
     */
    public static String toJsonString(Object object) {
        return JSON.toJSONString(object);
    }

    /**
     * 判断字符串是否是jsonobject
     * @param str
     * @return
     */
    public static boolean isJsonObject(String str){
        try{
            JSONObject jsonObject = JSON.parseObject(str);
            return true;
        } catch (Exception e){

            return false;
        }
    }

    /**
     * 判断字符串是否是jsonarray
     * @param str
     * @return
     */
    public static boolean isJsonArray(String str){
        try{
            JSONArray jsonArray = JSON.parseArray(str);
            return true;
        } catch (Exception e){
            return false;
        }
    }

    public static ValueFilter filter = new ValueFilter() {
        @Override
        public Object process(Object obj, String s, Object v) {
            if (v == null) {
                return "";
            }
            return v;
        }
    };
}
